﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TianYuan.Utils.PagerHelper;
using Newtonsoft.Json;

namespace TianYuan.Utils.Data
{
    /// <summary>
    /// 响应结果类
    /// </summary>
    public class R
    {
        /// <summary>
        /// 是否成功
        /// </summary>
        public bool  success
        {
            get;
            set;
        }
        /// <summary>
        /// http状态码：200,401,500等等
        /// </summary>
        public int code { set; get; }
        /// <summary>
        /// 消息
        /// </summary>
        public string message
        {
            get;
            set;
        }

        /// <summary>
        /// 新ID值
        /// </summary>
        public dynamic newid
        {
            get;
            set;
        }
        /// <summary>
        /// 数据
        /// </summary>
        public object data
        {
            get;
            set;
        }

        /// <summary>
        /// 消息额外数据
        /// </summary>
        public object extradata
        {
            get;
            set;
        }

        /// <summary>
        /// 响应消息封装类
        /// </summary>
        /// <param name="message">消息内容</param>
        /// <returns></returns>
        public static R Default(string message = null)
        {
            var result = new R();
            result.success = true;
            result.code = 200;
            result.message = message;
            return result;
        }

        /// <summary>
        /// 成功消息
        /// </summary>
        /// <param name="Status">状态:1-成功；0-缺省; -1失败</param>
        /// <param name="Message">消息内容</param>
        /// <returns></returns>
        public static R Success(string message = null)
        {
            var result = new R();
            result.code = 200;
            result.success = true;
            result.message = message;

            return result;
        }

        /// <summary>
        /// 响应消息封装类，用于插入操作，返回新ID
        /// </summary>
        /// <returns></returns>
        public static R Success(dynamic newId, string message = null)
        {
            var result = new R();
            result.newid = newId;
            result.code = 200;
            result.success = true;
            result.message = message;

            return result;
        }
        /// <summary>
        /// 响应消息封装类
        /// </summary>
        /// <param name="errorMessage"></param>
        /// <returns>code = 200,success = false</returns>
        public static R Warn(string errorMessage)
        {
            var result = new R();
            result.code = 200;
            result.success = false;
            result.message = errorMessage;
            return result;
        }
        /// <summary>
        /// 响应消息封装类
        /// </summary>
        /// <param name="errorMessage"></param>
        /// <returns>code = 500,success = false</returns>
        public static R Error(string errorMessage)
        {
            var result = new R();
            result.code = 500;
            result.success = false;
            result.message = errorMessage;
            return result;
        }

        /// <summary>
        /// 解析返回结果中的状态标志位
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static bool Parse(string message)
        {
            //var result = JsonSerializer.DeserializeFromString<ResponseResult>(message);
            var result = JsonConvert.DeserializeObject<R>(message);
            if (result != null)
            {
                var success = result.code == 200;
                return success;
            }
            return false;
        }

        public static R Deserialize(string message)
        {
            var result = JsonConvert.DeserializeObject<R>(message);
            return result;
        }
    }

    /// <summary>
    /// 响应消息类(泛型)
    /// </summary>
    public class R<T> where T : class
    {

        /// <summary>
        /// 是否成功
        /// </summary>
        public bool success
        {
            get;
            set;
        }
        /// <summary>
        /// http状态码：200,401,500等等
        /// </summary>
        public int code { set; get; }
        /// <summary>
        /// 消息
        /// </summary>
        public string message
        {
            get;
            set;
        }

        /// <summary>
        /// 新ID值
        /// </summary>
        public dynamic newid
        {
            get;
            set;
        }
        /// <summary>
        /// 业务实体
        /// </summary>
        public T data
        {
            get;
            set;
        }

        /// <summary>
        /// 消息额外数据
        /// </summary>
        public object extradata
        {
            get;
            set;
        }

        
        /// <summary>
        /// 总页数
        /// </summary>
        public int totalpages
        {
            get;
            set;
        }

        /// <summary>
        /// 总行数
        /// </summary>
        public int totalrowscount
        {
            get;
            set;
        }

        /// <summary>
        /// 响应消息封装类
        /// </summary>
        /// <returns></returns>
        public static R<T> Default()
        {
            var result = new R<T>();
            result.success = true;
            result.data = null;
            result.code = 200;
            result.message = string.Empty;
            return result;
        }
        /// <summary>
        /// 返回结果实体<see cref="R"/>类型的新实例
        /// </summary>
        /// <param name="t">结果实体<see cref="R"/>类型的实例</param>
        /// <param name="Message">消息内容</param>
        /// <returns></returns>
        public static R<T> Success(T t, string message = null)
        {
            var result = new R<T>();
            result.success = true;
            result.data = t;
            result.code = 200;
            result.message = message;
            return result;
        }
        /// <summary>
        /// 返回结果实体<see cref="R"/>类型的分页
        /// </summary>
        /// <param name="t">结果实体<see cref="R"/>类型的实例</param>
        /// <param name="message">消息</param>
        /// <returns></returns>
        public static R<PagerList<T>> Success(PagerList<T> t, string message = null)
        {
            var result = new R<PagerList<T>>();
            result.success = true;
            result.data = t;
            result.code = 200;
            result.message = message;
            result.totalpages = t.PageCount;
            result.totalrowscount = t.TotalCount;
            return result;
        }


        /// <summary>
        /// 返回成功消息，用于插入操作，返回新ID
        /// </summary>
        /// <param name="newId">返回新插入的id</param>
        /// <param name="message">消息内容</param>
        /// <returns></returns>
        public static R<T> Success(int newId, string message = null)
        {
            var result = new R<T>();
            result.success = true;
            result.newid = newId;
            result.data = null;
            result.code = 200;
            result.message = message;
            return result;
        }
        /// <summary>
        /// 返回警告类消息
        /// </summary>
        /// <returns>code = 200，success = false </returns>
        public static R<T> Warn(string errorMessage)
        {
            var result = new R<T>();
            result.code = 200;
            result.success = false;
            result.message = errorMessage;
            return result;
        }
        /// <summary>
        /// 返回错误消息
        /// </summary>
        /// <param name="message">消息内容</param>
        /// <returns></returns>
        public static R<T> Error(string message = null)
        {
            var result = new R<T>();
            result.success = false;
            result.code = 500;
            result.message = message;
            return result;
        }
        /// <summary>
        /// 解析返回结果中的状态标志位
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static bool Parse(string message)
        {
            //var result = JsonSerializer.DeserializeFromString<ResponseResult>(message);
            var result = JsonConvert.DeserializeObject< R< T>>(message);
            if (result != null)
            {
                var success = result.code == 200;
                return success;
            }
            return false;
        }

        /// <summary>
        /// Http 响应消息反序列化类
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static R<T> Deserialize(string message)
        {
            var response = JsonConvert.DeserializeObject<R<T>>(message);
            //ResponseResult<T> obj = message.ToObject<ResponseResult<T>>();
            return response;
        }

    }
}
